package com.khoayang.littlefighterchaos.game;

import org.cocos2d.actions.interval.CCAnimate;
import org.cocos2d.nodes.CCAnimation;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGRect;
import org.w3c.dom.Node;

import com.khoayang.littlefighterchaos.game.CharacterGame.Direction;

public class AttackingObject {
	private CCSprite _Sprite;
	private CGPoint _Position;
	private CCAnimation _Animation;
	private float _AttackValue;
	private float _AnimationDelay;
	private float _Velocity;
	private Direction _Direction = Direction.LEFT;
	private int _Owner;
	private float _AliveTime;
	private String _BeHitActionName;
	private CGPoint _StartPoint = CGPoint.ccp(0,0);

	public CCSprite get_Sprite() {
		return _Sprite;
	}

	public void set_Sprite(CCSprite _Sprite) {
		this._Sprite = _Sprite;
	}

	public CGPoint get_Position() {
		return _Position;
	}

	public void set_Position(CGPoint _position) {
		this._Position = _position;
		if (_position != null)
			_Sprite.setPosition(_position);
	}

	public float get_AttackValue() {
		return _AttackValue;
	}

	public void set_AttackValue(float _AttackValue) {
		this._AttackValue = _AttackValue;
	}

	public CCAnimation get_Animation() {
		return _Animation;
	}

	public void set_Animation(CCAnimation _Animation) {
		this._Animation = _Animation;
	}

	public void readFromXMLNode(Node node) {
		for (int i = 0; i < node.getChildNodes().getLength(); i++) {
			Node nodeTemp = node.getChildNodes().item(i);
			String nodeName = nodeTemp.getNodeName();
			if (nodeName.compareTo("Sprite") == 0) {
				_Sprite = CCSprite.sprite(nodeTemp.getTextContent());
			} else if (nodeName.compareTo("AttackingValue") == 0) {
				_AttackValue = Float.parseFloat(nodeTemp.getTextContent());
			} else if (nodeName.compareTo("AnimationDelay") == 0) {
				set_AnimationDelay(Float.parseFloat(nodeTemp.getTextContent()));
			} else if (nodeName.compareTo("Velocity") == 0) {
				set_Velocity(Float.parseFloat(nodeTemp.getTextContent()));
			} else if (nodeName.compareTo("AliveTime") == 0) {
				_AliveTime = Float.parseFloat(nodeTemp.getTextContent());
			} else if (nodeName.compareTo("Animation") == 0) {
				_Animation = getAnimationFromXMLNode(nodeTemp);
			} else if (nodeName.compareTo("BeHitActionName") == 0) {
				set_BeHitActionName(nodeTemp.getTextContent());
			}else if (nodeName.compareTo("Direction") == 0) {
				int dir = Integer.parseInt(nodeTemp.getTextContent());
				if(dir == 0)//cung huong tan cong
					_Direction = Direction.LEFT;
				else//nguoc huong tan cong
					_Direction = Direction.RIGHT;
			}else if(nodeName.compareTo("StartPoint") == 0){
				_StartPoint = getPointFromXML(nodeTemp);
			}
		}
	}

	private CGPoint getPointFromXML(Node node) {
		CGPoint point = CGPoint.ccp(0, 0);
		for(int i=0; i<node.getChildNodes().getLength(); i++){
			Node nodeTemp = node.getChildNodes().item(i);
			String nodeName = nodeTemp.getNodeName();
			if(nodeName.compareTo("x") == 0){
				point.x = Float.parseFloat(nodeTemp.getTextContent());
			}else if(nodeName.compareTo("y") == 0){
				point.y = Float.parseFloat(nodeTemp.getTextContent());
			}
		}
		return point;
	}

	private CCAnimation getAnimationFromXMLNode(Node node) {
		CCAnimation animation = CCAnimation.animation("");
		for (int i = 0; i < node.getChildNodes().getLength(); i++) {
			Node nodeTemp = node.getChildNodes().item(i);
			String nodeName = nodeTemp.getNodeName();
			if (nodeName.compareTo("Frame") == 0) {
				animation.addFrame(nodeTemp.getTextContent());
			}
		}
		return animation;
	}

	private float time = 0;

	public void update() {
		CGPoint newPos = _Position;
		if (get_Direction() == Direction.LEFT)// left
			newPos.x -= get_Velocity();
		else
			newPos.x += get_Velocity();
		set_Position(newPos);
		time++;
	}

	public Direction get_Direction() {
		return _Direction;
	}

	public void set_Direction(Direction _Direction) {
		this._Direction = _Direction;
	}

	public void runAnimation() {
		CCAnimate animate = CCAnimate.action(get_AnimationDelay(), _Animation,
				true);
		//CCRepeatForever repeatFoverer = CCRepeatForever.action(animate);
		_Sprite.runAction(animate);
		if (_Direction == Direction.LEFT) {
			_Sprite.setScaleX(-_Sprite.getScaleX());
		}
	}

	public int get_Owner() {
		return _Owner;
	}

	public void set_Owner(int _Owner) {
		this._Owner = _Owner;
	}

	public AttackingObject clone() {
		AttackingObject attackingObject = new AttackingObject();
		attackingObject.set_Sprite(CCSprite.sprite(_Sprite.getTexture()));
		attackingObject.set_Animation(_Animation);
		attackingObject.set_Direction(_Direction);
		attackingObject.set_Owner(_Owner);
		attackingObject.set_AttackValue(_AttackValue);
		attackingObject.set_Position(_Position);
		attackingObject.set_Velocity(_Velocity);
		attackingObject.set_AnimationDelay(_AnimationDelay);
		attackingObject.set_AliveTime(_AliveTime);
		attackingObject.set_BeHitActionName(_BeHitActionName);
		attackingObject.set_StartPoint(CGPoint.ccp(_StartPoint.x, _StartPoint.y));
		return attackingObject;
	}

	public float get_Velocity() {
		return _Velocity;
	}

	public void set_Velocity(float _Velocity) {
		this._Velocity = _Velocity;
	}

	public float get_AnimationDelay() {
		return _AnimationDelay;
	}

	public void set_AnimationDelay(float _AnimationDelay) {
		this._AnimationDelay = _AnimationDelay;
	}

	public float get_AliveTime() {
		return _AliveTime;
	}

	public void set_AliveTime(float _AliveTime) {
		this._AliveTime = _AliveTime;
	}

	public boolean isDied() {
		if (time >= _AliveTime)
			return true;
		return false;
	}

	public void isDied(boolean b) {
		if (b)
			time = _AliveTime;
	}

	public String get_BeHitActionName() {
		return _BeHitActionName;
	}

	public void set_BeHitActionName(String _BeHitActionName) {
		this._BeHitActionName = _BeHitActionName;
	}

	public CGPoint get_StartPoint() {
		return _StartPoint;
	}

	public void set_StartPoint(CGPoint _StartPoint) {
		this._StartPoint = _StartPoint;
	}
	
	public void setOrder()
	{
		_Sprite.getParent().reorderChild(_Sprite, 2000 - (int)(_Position.y - _Sprite.getContentSize().height/2));
	}

	public CGRect getRect()
	{
		CGRect rect = CGRect.make(_Sprite.getPosition().x - _Sprite.getContentSize().width/2, _Sprite.getPosition().y - _Sprite.getContentSize().height/2, 
				_Sprite.getContentSize().width, _Sprite.getContentSize().width);
		return rect;
	}
	
}
